package com.doit.day03

import scala.io.Source

object _05_join案例 {
  def main(args: Array[String]): Unit = {
    /*
    order.txt
    order011,u001,300
    order012,u002,200
    order023,u006,100
    order056,u007,300
    order066,u003,500
    order055,u004,300
    order021,u005,300
    order014,u001,100
    order025,u005,300
    order046,u007,30
    order067,u003,340
    order098,u008,310

    user.txt
    u001,hls,22,fengjie
    u002,wangwu,31,lisi
    u003,zhangyanru,22,tananpengyou
    u004,laocao,26,fengyi
    u005,mengqi,12,nvmengqi
    u006,haolei,38,sb
    u007,wanghongjing,24,wife
    u009,wanghongjing,24,wife

    order011,u001,300,hls,22,fengjie    join   left join
     */
    //order021,u005,300
    val order: List[String] = Source.fromFile("data/order.txt").getLines().toList
    //u001,hls,22,fengjie
    val user: List[String] = Source.fromFile("data/user.txt").getLines().toList

    val orders: List[(String, String, String)] = order.map(line => {
      //order021,u005,300
      val arr: Array[String] = line.split(",")
      (arr(0), arr(1), arr(2))
    })


    val users :List[(String,(String,String,String))]= user.map(line => {
      //u001,hls,22,fengjie
      val arr: Array[String] = line.split(",")
      (arr(0),(arr(1) , arr(2),arr(3)))
    })

    val map: Map[String, (String, String, String)] = users.toMap

    for (o <- orders) {
      val maybeTuple: Option[(String, String, String)] = map.get(o._2)
      if (maybeTuple.isDefined){
        println((o,maybeTuple.get))
      }
    }



    //join
/*    for (o <- orders) {
      for (u <- users) {
        if(o._2.equals(u._1)){
          println((o._1, o._2, o._3, u._2, u._3, u._4))
        }
      }
    }*/



    //left join




  }

}
